.TH std::swap(std::basic_string) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::swap(std::basic_string) \- std::swap(std::basic_string)

.SH Synopsis
   Defined in header <string>
   template< class CharT, class Traits, class Alloc >

   void swap( std::basic_string<CharT, Traits, Alloc>& lhs,     \fI(until C++17)\fP

              std::basic_string<CharT, Traits, Alloc>& rhs );
   template< class CharT, class Traits, class Alloc >

   void swap( std::basic_string<CharT, Traits, Alloc>& lhs,     \fI(since C++17)\fP
                                                                (constexpr since C++20)
              std::basic_string<CharT, Traits, Alloc>& rhs )
   noexcept(/* see below */);

   Specializes the std::swap algorithm for std::basic_string. Swaps the contents of lhs
   and rhs.

   Equivalent to lhs.swap(rhs).

.SH Parameters

   lhs, rhs - strings whose contents to swap

.SH Return value

   \fI(none)\fP

.SH Complexity

   Constant.

.SH Exceptions
                                     \fI(since C++17)\fP
   noexcept specification:
   noexcept(noexcept(lhs.swap(rhs)))

.SH Example


// Run this code

 #include <iostream>
 #include <string>

 int main()
 {
     std::string a = "AAA";
     std::string b = "BBBB";

     std::cout << "Before swap:\\n"
                  "a = " << a << "\\n"
                  "b = " << b << "\\n\\n";

     std::swap(a, b);

     std::cout << "After swap:\\n"
                  "a = " << a << "\\n"
                  "b = " << b << '\\n';
 }

.SH Output:

 Before swap:
 a = AAA
 b = BBBB

 After swap:
 a = BBBB
 b = AAA

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to              Behavior as published              Correct behavior
   LWG 2064 C++11      non-member swap was noexcept and inconsistent   noexcept removed
                       with member swap

.SH See also

   swap swaps the contents
        \fI(public member function)\fP

.SH Category:
     * conditionally noexcept
